<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="24">
					<el-form-item label="退租企业" prop="enterprise_id"  >
						<el-select v-model="form.enterprise_id" class="m-2" placeholder="企业名称" style="width: 100%;" collapse-tags collapse-tags-tooltip :max-collapse-tags="3" clearable>
							<el-option
								v-for="item in epList"
								:key="item.enterprise_id"
								:label="item.enterprise_id"
								:value="item.enterprise_id"
							/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :lg="24">
					<el-form-item label="时间" prop="checkout_date"  style="width: 40%;">
						<el-date-picker v-model="form.checkout_date" :disabled-date="publishDateAfter" placeholder="退租日期" type="date" value-format="YYYY-MM-DD HH:mm" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
                <el-col :lg="24">
					<el-form-item label="清费证明" prop="attach_id">
						<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'ici_checkout'}" :showFileList="false" @success="addNewFile">
							<attach-menu v-model:attachIds="form.attach_id" :roleFlag="4" :preview="true"></attach-menu>
							<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
                </sc-upload-file>
					</el-form-item>
				</el-col>
				<el-col :lg="24">
					<el-form-item label="负责人" prop="user_join">
						<select-user-dialog v-model:accountId="form.user_join" :mode="true"/>
					</el-form-item>
				</el-col>
			</el-row>

            <el-form-item label="情况说明" prop="notepad">
				<sc-editor v-model="form.notepad" placeholder="请输入" :templates="templates" :height="200" clearable style="width: 100%;"></sc-editor>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">确 定</el-button>
		</template>
	</el-dialog>

    <el-dialog :title="Title" :visible.sync="open" width="600px" append-to-body
           :close-on-click-modal=false>
</el-dialog>


</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import {defineAsyncComponent} from "vue";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			// addTemplate: {
			// 	park_id: 0,
			// 	park_name: "",
			// 	area_id: 0,
			// 	area_name: "",
			// 	building_id: 0,
			// 	building_name: "",
			// 	floor_id: 0,
			// 	floor_name: "",
			// 	space_id: 0,
			// 	space_name: "",
			// 	room_size: 0,
			// 	rental_pricing: "",
			// },
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			floorList:[],
			roomList:[],
			deviceList:[],
			mode: "add",
			titleMap: {
				add: '退租登记',

			},
			epList:[],
			fileList: [],
			apiObj: this.$API.file.attach.uploadFile,
			templates: [],
			form: {
				enterprise_id: '',
				checkout_id: '',
				checkout_date: '',
				attach_id: '',
				notepad: '',
				create_time: '',
				user_join: ''
			},
			spaceOptions: [],
			spaceOriginOptions: [],
			formSpace:{},
			formPackage:{},
			formPS:{},
			rules: {
				subject: [
					{required: true, message: '预约摘要不能为空', trigger: 'blur'}
				]
			}
            ,
		}
    

	},
	mounted() {
		//this.getEPList();
	},
	methods: {
		async remoteMethod(keyword) {
			let res = await this.$API.crm.crmCustomer.getAllCustomerListForSelect.get({keyword: keyword});
			if (res.code == 200) {
				this.options = res.data;
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
        handleUpload(){

        },
		async getEPList(bId)
		{
			var res = await this.$API.attractinvestment.checkin.getCheckinList.get({buildingId:bId});
			if (res.code == 200) {
				this.epList = res.data.list;
			}
		},
		
		isValid(data){
			if (Object.prototype.toString.call(data) === '[object Undefined]') {
				return false
			}
			if (Object.prototype.toString.call(data) === '[object Null]') {
				return false
			}
			return true
		},
		
		async remoteSpaceMethod(keyword) {
			console.log(this.form.building_id)
			// const building_id = this.isValid(this.form.building_id) ? this.form.building_id : 12; //开发模式
				// 如果keyword开头为0，则删除开头多余的0
			if(keyword.startsWith('0')){
				keyword = keyword.substr(1);
			}
			if(keyword.length>0){
				console.log('keyword is ', keyword)
				this.spaceOptions = this.spaceOriginOptions.filter(e=>{return e.room.includes(keyword)})
			}else{
				console.log('keyword is null')
				this.spaceOptions = this.spaceOriginOptions;
			}
		},
		
		publishDateAfter(time){
			return time.getTime()<(Date.now()-24*60*60*1000)
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.attractinvestment.checkoutManage.createCheckout.post(this.form);
						res = await this.$API.attractinvestment.checkin.updateCheckin.post({"enterprise_id": this.form.enterprise_id, "checkin_status":2})
					} else if (this.mode == "edit") {
						res = await this.$API.attractinvestment.checkoutManage.updateCheckout.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		
		setCreateData(data,roomList)
		{
			this.form.enterprise_id = data.enterprise_id;
			this.form.checkout_id = data.checkout_id;
			this.form.checkout_date = data.checkout_date;
			this.form.attach_id = data.attach_id;
			this.form.notepad = data.notepad;
			
			this.form.create_time = data.create_time;
			this.form.user_join = data.user_join;
			console.log(data)
			this.getEPList(data.building.building_id);
		},
		setData(data,roomList) {
			this.form.enterprise_id = data.enterprise_id;
			this.form.checkout_id = data.checkout_id;
			this.form.checkout_date = data.checkout_date + "T00:00:00.000+00:00";
			this.form.attach_id = data.attach_id;
			this.form.notepad = data.notepad;
			
			this.form.create_time = data.create_time;
			this.form.user_join = data.user_join;
		},
	

		addNewFile(res, file) {
			let tArr = [];
			if (this.form.attach_id !== undefined && this.form.attach_id != null && this.form.attach_id !== '') {
				tArr = this.form.attach_id.split(",");
			}
			tArr.push(res.data.attachId);
			this.form.attach_id = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
